/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.IOException;

import util.IPAddressFormatter;
import util.Message;
import network.UdpClient;

/**
 *
 * @author pol
 */
public class MainFrame extends JFrame {

    public MainFrame() {
        initComponents();
        try {
            client = new UdpClient(InetAddress.getByName(ipAddressTextField.getText()),
                    Integer.parseInt(portTextField.getText()));
            client.run();
        } catch (UnknownHostException uhe) {
            uhe.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void initComponents() {
        GridBagConstraints gridBagConstraints;
        ipAddressTextField = new JFormattedTextField(new IPAddressFormatter());
        sendDataTextField = new JTextField();
        jScrollPane1 = new JScrollPane();
        receiveTextArea = new JTextArea();
        portTextField = new JTextField();
        ipAddressLabel = new JLabel();
        portLabel = new JLabel();
        sendButton = new JButton();

        getContentPane().setLayout(new GridBagLayout());

        ipAddressTextField.setValue(new byte[]{(byte) 127, 0, 0, 1});

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 108;
        gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new Insets(6, 6, 0, 0);
        getContentPane().add(ipAddressTextField, gridBagConstraints);


        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.ipadx = 228;
        gridBagConstraints.ipady = 41;
        gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new Insets(6, 6, 0, 0);
        getContentPane().add(sendDataTextField, gridBagConstraints);

        receiveTextArea.setColumns(20);
        receiveTextArea.setRows(5);
        jScrollPane1.setViewportView(receiveTextArea);

        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 5;
        gridBagConstraints.fill = GridBagConstraints.BOTH;
        gridBagConstraints.ipadx = 219;
        gridBagConstraints.ipady = 233;
        gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new Insets(6, 6, 0, 0);
        getContentPane().add(jScrollPane1, gridBagConstraints);

        portTextField.setText("8080");
       
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 32;
        gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new Insets(6, 6, 0, 0);
        getContentPane().add(portTextField, gridBagConstraints);

        ipAddressLabel.setText("IPv4 адрес");
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new Insets(12, 6, 0, 0);
        getContentPane().add(ipAddressLabel, gridBagConstraints);

        portLabel.setText("Порт");
         try {
            InetAddress thisIp = InetAddress.getLocalHost();
            portLabel.setText(thisIp.getHostAddress());
        } catch (Exception e) {
            e.printStackTrace();
        }
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new Insets(12, 6, 0, 0);
        getContentPane().add(portLabel, gridBagConstraints);

        sendButton.setText("Отправить");
        sendButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                sendButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.ipadx = 79;
        gridBagConstraints.ipady = 40;
        gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new Insets(7, 6, 13, 0);
        getContentPane().add(sendButton, gridBagConstraints);

        pack();
    }

    public static void setMessage(Message m) {
        receiveMessage = m;
        receiveTextArea.append(receiveMessage.toString() + "\n");
    }


    private void sendButtonActionPerformed(ActionEvent evt) {
        try {
            client.sendMessage(InetAddress.getByName(ipAddressTextField.getText()),
                    Integer.parseInt(portTextField.getText()), sendDataTextField.getText());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private JButton sendButton;
    private JFormattedTextField ipAddressTextField;
    private JLabel ipAddressLabel;
    private JLabel portLabel;
    private JScrollPane jScrollPane1;
    private static JTextArea receiveTextArea;
    private JTextField sendDataTextField;
    private JTextField portTextField;
    private static Message receiveMessage;
    private UdpClient client;
}
